#include <iostream>
#include <mutex>
#include <thread>

using std::cout;
using std::endl;
using std::mutex;
using std::thread;

mutex mtx;//创建了锁的对象

int gCnt = 0;

void threadFunc()
{
    for(size_t idx = 0; idx != 1000000; ++idx)
    {
        //std::mutex使用的时候，一定要注意上锁与
        //解锁程度出现，如果只上锁不解锁，那么
        //就会有卡死的风险
        mtx.lock();
        ++gCnt;
        mtx.unlock();
    }
}

int main(int argc, char *argv[])
{
    thread thA(threadFunc);
    thread thB(threadFunc);

    thA.join();
    thB.join();

    cout << "gCnt = " << gCnt << endl;
    return 0;
}

